
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="org.apache.commons.fileupload.FileItem"%> 
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@ page language="java" import="java.util.*,news.dao.*,news.entity.*,utilu.*"
	pageEncoding="utf-8"%>
<!-- 这个文档用来处理编辑的 和增加的新闻的 -->
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'domodifynew.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<%
		request.setCharacterEncoding("utf-8");
		News news = new News();
		//获取路径
		String patht=application.getRealPath("img");
		DiskFileItemFactory df=new DiskFileItemFactory();
		  ServletFileUpload sifle=new ServletFileUpload(df);
		  List<FileItem> file=sifle.parseRequest(request);
		  for(FileItem fi:file){
		  if(fi.isFormField()){
		  String sf=fi.getFieldName();
		if(sf.equals("nsummary")){
		String nsummary = fi.getString("utf-8");
		news.setNsummary(nsummary);
		}else if(sf.equals("ntid")){
		int ntid=Integer.parseInt(fi.getString("utf-8"));
		news.setNtid(ntid);	
		}else if(sf.equals("ncontent")){
		String ncontent = fi.getString("utf-8");
		news.setNcontent(ncontent);
		}else if(sf.equals("ntitle")){
		String ntitle = fi.getString("utf-8");
		news.setNtitle(ntitle);
		}else if(sf.equals("nauthor")){		
		String nauthor = fi.getString("utf-8");
		news.setNauthor(nauthor);
		}else if(sf.equals("nid")){		
		String nidt = fi.getString("utf-8");
		int nid = Integer.parseInt(nidt);
		news.setNid(nid);
		}
		  }else{
		  //获取新闻的名称
		  String npicPath;
		  npicPath=request.getParameter("nidd");
		  if(!(fi.getName().equals(""))){
		 npicPath=fi.getName();
		 File fii=new File(patht,npicPath);
		 fi.write(fii);
		  }
		 
		  news.setNpicPath(npicPath);
		  
		  
		  }
	  }
		
		NewsDao nw = new NewsDao();
		
		//修改时间
		Date da = new Date();
		news.setNmodifyDate(DBUtil.datestring(da));
//添加时间
		 news.setNcreateDate(DBUtil.datestring(da));
	
	//获取请求判断是添加还是修改
	String claim=request.getParameter("claim");
	if(claim.equals("modifynew")){
	//修改新闻

	int bo=	nw.amendnews(news);
	if(bo>0){%>
	<script type="text/javascript">
	alert("修改成功！");location = "admin.jsp";
	</script>
	
<%	}else{%>
	<script type="text/javascript">
	alert("修改失败！");location = "admin.jsp";
	</script>
	<% }
		}else{
			int boo=nw.addnews(news);
	if(boo>0){%>
	<script type="text/javascript">
	alert("添加成功！");location = "admin.jsp";
	</script>
	
<%	}else{%>
	<script type="text/javascript">
	alert("添加失败！");location = "admin.jsp";
	</script>
	<% }
		
		
		
		
		}
	%>

</body>
</html>
